//
//  NewFeatureController.swift
//  新浪weibo
//
//  Created by test on 15/8/2.
//  Copyright © 2015年 test. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"


class NewFeatureController: UICollectionViewController {
    //
    private let imageCount = 4
    
    private let layout = ZYLayout()
  //每次实例化的时候调用
    init() {
     super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.imageIndex = indexPath.item
        
        return cell
    }
    /// 完成显示 cell - indexPath 是之前消失 cell 的 indexPath
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 获取当前显示的 indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        
        // 判断是否是末尾的 indexPath
        if path.item == imageCount - 1 {
            // 播放动画
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            cell.startButtonAnim()
        }
    }

}

class NewFeatureCell: UICollectionViewCell {
    
    private var imageIndex:Int = 0 {
    
        didSet {
        startButton.hidden = true
        iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        
        }
  
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    func clickStartButton() {
       NSNotificationCenter.defaultCenter().postNotificationName(ZYRootViewControllerSwitchNotification, object: true)
    }
    
    /// 开始按钮动画
    private func startButtonAnim() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        // 禁用按钮操作
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 恢复默认形变
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                // 启用按钮点击
                self.startButton.userInteractionEnabled = true
        }
    }

    
    private func setUpUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        iconView.ff_Fill(contentView)
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
   
    }
    
    lazy private var iconView = UIImageView()
    lazy private var startButton: UIButton = {
    
    let button = UIButton()
       
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        // 根据背景图片自动调整大小
        button.sizeToFit()
        
        // 提示: 按钮的隐藏属性会因为复用工作不正常
        button.hidden = true
        
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button

    
    
    }()
    
}

private class ZYLayout: UICollectionViewFlowLayout {
    
    private override func prepareLayout() {
        itemSize = collectionView!.bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
     
        
    }
    
}
